package com.ac.input.web.action.input;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.caac.core.web.action.BaseAction;
import com.caac.core.web.form.BaseForm;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright AgileCentury 2004</p>
 * <p>Company: AgileCentury</p>
 * @author <a href="mailto:lianzs@tom.com">Lianzs</a>
 * @version 1.0
 */

public class ValidateCodeAction extends BaseAction {
    protected ActionForward doExecute(ActionMapping mapping, BaseForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response) throws
            Exception {
        String code = request.getParameter("code");
        String listSql = request.getParameter("listSql");
        int iCount = 0;
        if (listSql != null && !"".equals(listSql)) {
            listSql = listSql.toLowerCase().trim();

            int iPos = listSql.indexOf("from");
            String strTemp = listSql.substring(7, iPos);
            String[] colNames = strTemp.split(",");

            String strSql = "Select count(1) from (" + listSql + ") where "
                            + colNames[0] + "=?";
            iCount = this.getJdbcTemplate().queryForInt(strSql,
                    new String[] {code});
        }

        response.reset();
        if (iCount > 0) {
            response.getOutputStream().write("1".getBytes());
        } else {
            response.getOutputStream().write("0".getBytes());
        }
        return null;
    }
}
